const axios = require('axios');

(async () => {
  try {
    // 1. 登录
    const loginResponse = await axios.post('http://localhost:3000/api/auth/login', {
      email: 'admin@campus.com',
      password: 'admin123'
    });
    
    const authToken = loginResponse.data.token;
    const userId = loginResponse.data.user.id;
    console.log(`✅ 登录成功，用户ID: ${userId}`);
    
    // 2. 详细检查活动21、20、28的勋章
    const problemActivityIds = [20, 21, 28];
    
    for (const activityId of problemActivityIds) {
      console.log(`\n🔍 检查活动 ${activityId}:`);
      
      // 获取全部勋章，检查这个活动
      const allResponse = await axios.get('http://localhost:3000/api/badges', {
        headers: { Authorization: `Bearer ${authToken}` }
      });
      
      const allBadgesForActivity = allResponse.data.badges.filter(b => b.activity_id === activityId);
      console.log(`  全部勋章中此活动的勋章数: ${allBadgesForActivity.length}`);
      allBadgesForActivity.forEach((badge, i) => {
        console.log(`    ${i+1}. ID=${badge.id}, 状态=${badge.status}, 用户=${badge.user_id}, 拥有=${badge.isOwned}`);
      });
      
      // 获取拥有的勋章，检查这个活动
      const ownedResponse = await axios.get('http://localhost:3000/api/badges?filter=owned', {
        headers: { Authorization: `Bearer ${authToken}` }
      });
      
      const ownedBadgesForActivity = ownedResponse.data.badges.filter(b => b.activity_id === activityId);
      console.log(`  拥有勋章中此活动的勋章数: ${ownedBadgesForActivity.length}`);
      ownedBadgesForActivity.forEach((badge, i) => {
        console.log(`    ${i+1}. ID=${badge.id}, 状态=${badge.status}, 用户=${badge.user_id}, 拥有=${badge.isOwned}`);
      });
      
      // 获取可获得勋章，检查这个活动
      const availableResponse = await axios.get('http://localhost:3000/api/badges?filter=available', {
        headers: { Authorization: `Bearer ${authToken}` }
      });
      
      const availableBadgesForActivity = availableResponse.data.badges.filter(b => b.activity_id === activityId);
      console.log(`  可获得勋章中此活动的勋章数: ${availableBadgesForActivity.length}`);
      availableBadgesForActivity.forEach((badge, i) => {
        console.log(`    ${i+1}. ID=${badge.id}, 状态=${badge.status}, 用户=${badge.user_id}, 拥有=${badge.isOwned}`);
      });
    }
    
  } catch (error) {
    console.error('调试失败:', error.message);
  }
})(); 